# -*- coding: UTF-8 -*-

from odoorpc2 import RPC

# base_url = 'http://192.168.56.111:8069'
base_url = 'http://123.57.177.23:8069'
base_url = 'http://192.168.56.114:8069'

timeout = 50000

addons_list = ['odoorpc2/addons']


dbname = 'T2'
# dbname = 'T2'
username = 'admin'
password = '123456'

rpc = RPC(base_url, timeout, dbname, addons_list)


def test_action():
    print('test_action')
    action_id = 'base.action_country'
    action = rpc.env.action_get(action_id, from_odoo=True)
    print('test_action', action, action.info)

    action = rpc.env.action_get(action_id, from_odoo=False)
    print('test_action', action, action.info)


def test_treeview():
    print('test_treeview')
    action_id = 'base.action_country'
    tree = rpc.env.treeview(action_id)
    tree.load_fields()
    print('test_action tree', tree, tree.view_info)

    res = tree.search_read()
    print('test_action tree', res)


def test_formview():
    print('test_formview')
    action_id = 'base.action_country'
    tree = rpc.env.treeview(action_id)
    tree.load_fields()
    res = tree.search_read(domain=[('code', '=', 'CN')])
    print('test_formview tree', res)

    one = res[0]
    res_id = one['id']
    form = rpc.env.formview(action_id)
    form.load_fields()
    record = form.read(res_id)
    print('test_formview form', record)

    state_ids = record['state_ids']
    print('test_formview state_ids', state_ids)

    state_ids_field_info = form.fields['state_ids']
    print('test_formview state_ids_field_info', state_ids_field_info)
    state_ids_relation = rpc.env.relation(state_ids_field_info)
    print('test_formview state_ids_relation', state_ids_relation)
    state_ids_relation_views = state_ids_relation.load_views()
    print('test_formview state_ids_relation_views', state_ids_relation_views)

    state_ids_treeview = state_ids_relation.tree
    state_ids_records = state_ids_treeview.read(state_ids)
    print('test_formview state_ids_records', state_ids_records)

    state_ids_formview = state_ids_relation.form
    state_id = state_ids_records[0]['id']
    state_id_record = state_ids_formview.read(state_id)
    print('test_formview state_id_record', state_id_record)


def test():
    res = rpc.login(username,  password)
    # test_action()
    # test_treeview()
    test_formview()


test()
